using System.Collections.Generic;
using UnityEngine;

public class SGBackGroundDownlaodAndroid : SGBaseBackGroundDownloader
{
    
    private const string k_PackageName = "com.odin.backgrounddownload";
    private const string k_NativeClassName = k_PackageName + ".SGBackGroundDownloadManager";

    private const string k_MethodInit = "Init";
    private const string k_MethodForceDownload = "ForceDownload";
    private const string k_MethodAddDownload = "AddDownload";
    private const string k_MethodStartDownload = "StartDownload";
    private const string k_MethodPauseDownload = "PauseDownload";
    private const string k_MethodSetDownloadParams = "SetDownloadParams";
    private const string k_MethodGetDownloadedSize = "GetDownloadedSize";
    private const string k_MethodGetAllDownloadSize = "GetAllDownloadSize";
    
    public SGBackGroundDownlaodAndroid(BackGroundDownloadType downloadType)
    {
        BackGroundDownloadCallback callback = new BackGroundDownloadCallback();
        using (var jUnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
        {
            var jActivity = jUnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
            using (var javaSGBackGroundDownload = new AndroidJavaClass(k_NativeClassName))
            {
                javaSGBackGroundDownload.CallStatic(k_MethodInit, jActivity, NetWorkDefine.k_Url, Application.persistentDataPath, callback);
            }
        }
    }

    public override void ForceDownload(string urlRelativePath, string saveRelaitvepath, long fileSize)
    {
        using (var javaSGBackGroundDownload = new AndroidJavaClass(k_NativeClassName))
        {
            javaSGBackGroundDownload.CallStatic(k_MethodForceDownload, urlRelativePath, saveRelaitvepath, fileSize);
        }
    }

    public override void AddDownload(List<BackGroundDownloadParam> downloadParams)
    {
        var datas = ConvertParamsToBytes(downloadParams);

        using (var javaSGBackGroundDownload = new AndroidJavaClass(k_NativeClassName))
        {
            javaSGBackGroundDownload.CallStatic(k_MethodAddDownload, datas);
        }
    }

    public override void StartDownload()
    {
        using (var javaSGBackGroundDownload = new AndroidJavaClass(k_NativeClassName))
        {
            javaSGBackGroundDownload.CallStatic(k_MethodStartDownload);
        }
    }

    public override void PauseDownload()
    {
        using (var javaSGBackGroundDownload = new AndroidJavaClass(k_NativeClassName))
        {
            javaSGBackGroundDownload.CallStatic(k_MethodPauseDownload);
        }
    }
    public override void SetDownloadParam(int maxCount, float silentIntervalTime, int silentBatchSize)
    {
        using (var javaSGBackGroundDownload = new AndroidJavaClass(k_NativeClassName))
        {
            javaSGBackGroundDownload.CallStatic(k_MethodSetDownloadParams, maxCount, silentIntervalTime, silentBatchSize);
        }
    }

    public override long GetDownloadedSize()
    {
        using (var javaSGBackGroundDownload = new AndroidJavaClass(k_NativeClassName))
        {
            return javaSGBackGroundDownload.CallStatic<long>(k_MethodGetDownloadedSize);
        }
    }

    public override long GetAllDownloadSize()
    {
        using (var javaSGBackGroundDownload = new AndroidJavaClass(k_NativeClassName))
        {
            return javaSGBackGroundDownload.CallStatic<long>(k_MethodGetAllDownloadSize);
        }
    }
}